const app = getApp();
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../dist/base/index');
var pageNo = 1;
var keyword = '';
Page({
  data: {
    tip: "",
    loading: false,
    content: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.getIndexList(options.keyword);
  },
  details(e) {
    //详情页跳转
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../details/details?id=' + e.currentTarget.id
    })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showLoading({
      title: '刷新中',
    });
    //用户下拉刷新
    this.data.content = [];
    console.log('下拉刷新清空数据');
    console.log(this.data.content)
    this.getIndexList();
    //停止当前页面下拉刷新。
    wx.stopPullDownRefresh()
    setTimeout(function() {
      wx.hideLoading()
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    that.setData({
      loading: true,
      tip: "正在加载"
    })
    setTimeout(function() {
      that.setData({
        loading: false,
        tip: "没有更多数据"
      })
    }, 1000);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showLoading({
      title: '刷新中',
    });
    //用户下拉刷新
    this.data.content = [];
    this.getIndexList();
    //停止当前页面下拉刷新。
    wx.stopPullDownRefresh()
    setTimeout(function() {
      wx.hideLoading()
    }, 1500)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '空城丶Blog',
      path: '/pages/index/index'
    }
  },
  getIndexList: function(keyword) {
    var that = this;
    that.setData({
      loading: true,
      tip: "正在加载"
    });
    
    wx.request({
      url: util.basePath + '/article/v1/searchArticle',
      method: "post",
      data: {
        keyword: keyword
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            content: res.data.payload
          });
        } else {
          that.setData({ loading: false });
          $Toast({
            content: '未查询到文章数据!',
            type: 'warning'
          });
        }
      }
    });
  }
});